//
//  TVCouponCell.swift
//  iPhone
//
//  Created by Toj on 12/1/22.
//

import UIKit

class TVCouponCellModel: NSObject {
    var isSelected = false
    var couponCode = ""
    var tips = ""
    var discountTips = ""
    var discontPercentage: Double = 0
    private(set) var preferentialAmount: Double = 0
    private(set) var discontString: NSAttributedString?
    private(set) var dateFromTo = ""
    // 应用范围说明
    private(set) var orderAbove = ""
    // 优惠券对应商品规格描述
    private(set) var onlyForTime = ""
    
    private(set) var symbol = ""
    
    private(set) var couponType: CouponType = .others
    
    // 优惠券使用满足金额
    private(set) var amountThreshhold: Double = 0
    
    init(coupon: Coupon) {
        couponCode = coupon.promotion_coupon_instance_code
        
        let startTime = coupon.validStartTime
        let endTime = coupon.validEndTime
        if !startTime.isEmpty && !endTime.isEmpty {
            dateFromTo = startTime + "-" +  endTime
        }
        
        amountThreshhold = Double(coupon.amount_threshhold) ?? 0
        symbol = coupon.currency
        // 应用范围说明
        orderAbove = "coupon_rule".localized() + " \(symbol) " + coupon.application_guide
        // 优惠券对应商品规格描述
        onlyForTime = "coupon_range".localized() + " " + coupon.promotion_coupon_commodity_detail_info

        // 折扣
        //var discont = ""
        var discontAmount = ""
        couponType = coupon.coupon_type
        switch couponType {
        case .fullCut, .fullcut:
            preferentialAmount = Double(coupon.preferential_amount) ?? 0
            discontAmount = coupon.preferential_amount.st_cleanZero
            //discont = symbol + discontAmount
        case .discount, .discount2:
            var percentage = coupon.discount / 100
            if percentage > 1 {
                percentage = 1
            }
            discontPercentage = percentage
            let discount = coupon.discount.string.st_cleanZero
            discontAmount = "\(discount)%"
            //discont = discontAmount
        default:
            break
        }
        
        guard !discontAmount.isEmpty else { return }
        //tips = discont + "membership_CouponInformationPopup3".localized()
        let fontColor = "#CC9900".to_color
        let style = NSMutableParagraphStyle()
        style.lineBreakMode = .byTruncatingTail
        style.alignment = .left
        let attributed = symbol.to_attributedString(font: UIFont.boldSystemFont(ofSize: 14), color: fontColor, paragraph: style)
        let offValue = discontAmount.to_attributedString(font: UIFont.boldSystemFont(ofSize: 32), color: fontColor)
        attributed.append(offValue)
        discontString = attributed
    }
    
    func offPrice(_ price: Double) -> Double {
        var offPriceValue: Double = 0
        switch couponType {
        case .fullCut, .fullcut:
            offPriceValue = preferentialAmount
        case .discount, .discount2:
            offPriceValue = price * discontPercentage
        default:
            break
        }
        tips = String(offPriceValue) + "membership_CouponInformationPopup3".localized()
        let tipString = "membership_CouponListTip2".localized()
        discountTips = String(format: tipString, String(offPriceValue), String(amountThreshhold))
        return offPriceValue
    }
}

class TVCouponCell: UITableViewCell {
    
    var cellModel: TVCouponCellModel? { didSet {
        guard let model = cellModel else { return }
        discountsLabel.attributedText = model.discontString
        radioButton.isSelected = model.isSelected
        dateLabel.text = model.dateFromTo
        aboveLabel.text = model.orderAbove
        onlyForTimeLabel.text = model.onlyForTime
    } }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .white
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    private lazy var leftView = UIView()
    private lazy var rightView = UIView()
    
    private lazy var backgroundImageView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_tv_coupons_bgimg".to_image
        return view
    }()
    
    private lazy var radioButton: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.setImage("com_radio_white_nor".to_image, for: .normal)
        view.setImage("com_radio_white_sel".to_image, for: .selected)
        view.setBackgroundImage("commodity_tv_coupons_rightIcon".to_image, for: .normal)
        return view
    }()
    
    private lazy var discountsLabel: UILabel = {
        let view = UILabel()
        view.adjustsFontSizeToFitWidth = true
        view.textColor = "#CC9900".to_color
        return view
    }()
    
    private lazy var offLabel: UILabel = {
        let view = UILabel()
        view.text = "membership_CouponInformationPopup3".localized()
        view.font = UIFont.boldSystemFont(ofSize: 14.f_scale)
        view.textColor = "#CC9900".to_color
        view.sizeToFit()
        return view
    }()
    
    private lazy var dateLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont.systemFont(ofSize: 10.f_scale)
        view.textColor = "#99773C".to_color
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.text = "coupon"
        view.font = 20.ui_boldFont
        view.textColor = "#CC9900".to_color
        return view
    }()
    
    // 应用范围说明
    // e.g.: 【For order above .....】
    private lazy var aboveLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#99773C".to_color
        view.font = 10.to_systemFont
        return view
    }()
  
    // 优惠券对应商品规格描述
    // e.g.:【Only for ......】
    private lazy var onlyForTimeLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#99773C".to_color
        view.font = 10.to_systemFont
        return view
    }()
}

extension TVCouponCell {
    
    private func setupMakeAddSubviews() {
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(leftView)
        contentView.addSubview(rightView)
        // contentView.addSubview(rightImageView)
        contentView.addSubview(radioButton)
        
        leftView.addSubview(discountsLabel)
        leftView.addSubview(offLabel)
        leftView.addSubview(dateLabel)
        
        rightView.addSubview(titleLabel)
        rightView.addSubview(aboveLabel)
        rightView.addSubview(onlyForTimeLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        backgroundImageView.snp.makeConstraints { make in
            make.top.equalTo(contentView)
            make.height.equalTo(80)
            make.left.right.equalTo(contentView).inset(16)
        }
        
        leftView.snp.makeConstraints { make in
            make.left.equalTo(backgroundImageView)
            make.top.bottom.equalTo(backgroundImageView)
            make.width.equalTo(backgroundImageView).multipliedBy(0.457)
        }
        
        radioButton.snp.makeConstraints { make in
            make.right.equalTo(backgroundImageView)
            make.top.equalTo(3)
            make.width.equalTo(52)
            make.height.equalTo(72)
        }
        
        rightView.snp.makeConstraints { make in
            make.left.equalTo(leftView.snp.right)
            make.top.bottom.equalTo(leftView)
            make.right.equalTo(radioButton.snp.left)
        }
        
        dateLabel.snp.makeConstraints { make in
            make.bottom.equalTo(leftView.snp.bottom).offset(-20)
            make.left.equalTo(20)
        }
        
        discountsLabel.snp.makeConstraints { make in
            make.bottom.equalTo(dateLabel.snp.top)
            make.left.equalTo(dateLabel)
        }
        
        offLabel.snp.remakeConstraints { make in
            make.bottom.equalTo(discountsLabel.snp.bottom).offset(-4)
            make.left.equalTo(discountsLabel.snp.right)
            make.right.equalTo(leftView).inset(2)
            make.width.greaterThanOrEqualTo(offLabel.frame.width)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(13)
            make.left.equalTo(20)
            make.right.equalTo(rightView)
        }
        
        aboveLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.left.right.equalTo(titleLabel)
        }
        
        onlyForTimeLabel.snp.makeConstraints { make in
            make.top.equalTo(aboveLabel.snp.bottom)
            make.left.right.equalTo(titleLabel)
        }
    }
}
